package com.wn.controller;

import com.wn.common.util.JwtUtil;
import com.wn.model.vo.OrderVo;
import com.wn.model.vo.PayRequest;
import com.wn.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
@Api(tags = "订单模块")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private JwtUtil jwtUtil;
    /**
     * 创建订单
     */
    @PostMapping("/createOrder/{addressId}")
    @ApiOperation("创建订单")
    public ResponseEntity<String> createOrder(
            @RequestHeader(name = "Authorization", required = false) String token,
            @PathVariable Integer addressId) {
        //从token中获取userId
        Long userId = Long.valueOf(jwtUtil.getValue(token, "userId"));
        //调用业务层创建订单
        String orderNum = orderService.createOrder(userId, addressId);
        return ResponseEntity.ok("订单提交成功，订单号：" + orderNum);
    }

    /**
     * 查询订单
     */
    @GetMapping("/loadOrdersByStatus")
    @ApiOperation("查询指定状态的订单")
    public ResponseEntity<List<OrderVo>> loadOrdersByStatus(
            @RequestHeader(name = "Authorization", required = false) String token, Integer status) {
        //从token中获取userId
        Long userId = Long.valueOf(jwtUtil.getValue(token, "userId"));
        //调用业务层查询订单
        List<OrderVo> orderVos = orderService.queryOrder(userId, status);
        return ResponseEntity.ok(orderVos);
    }

    /**
     * 支付接口
     * @return 返回一个包含表单数据的HTML网页
     */
    @PostMapping("/pay")
    @ApiOperation("支付接口")
    public ResponseEntity<String> pay(@RequestBody PayRequest request) {
        //调用业务层支付
        String form = orderService.pay(Long.valueOf(request.getOrderId()));
        return ResponseEntity.ok(form);
    }
}